<template>
  <div style="padding: 10px">
    <!--    功能区-->
    <div style="margin: 10px 0px">
      <el-button type="primary" @click="submit">新增</el-button>
      <el-button type="primary" @click="setPrice">设置收费</el-button>
    </div>
    <!--    搜索区-->
    <div style="margin: 10px 0px">
      <el-input v-model="search" placeholder="请输入Id" style="width: 10%" clearable></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
    </div>
<!--    显示价格-->
    <hr>
    <div>
      <h4>费用信息:</h4>
      <div>
        <span>前8小时收取{{ price.initCost }}元，</span>
        <span>8-24小时收取{{ price.baseCost }}元，</span>
        <span>24小时后，每小时加收{{ price.addCost }}元</span>
      </div>
    </div>
    <hr>
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column prop="cab_id" label="id"  sortable />
<!--      <el-table-column prop="owner_id" label="所属管理员id" sortable />-->
      <el-table-column prop="sum" label="存储箱总数"  />
      <el-table-column prop="is_used" label="存储箱被使用数"  />
      <el-table-column prop="not_using" label="存储箱未被使用数" />
<!--      <el-table-column label="操作" width="350">-->
<!--&lt;!&ndash;        <template #default="scope">&ndash;&gt;-->
<!--        <template #default="scope">-->
<!--&lt;!&ndash;          <el-button type="primary"  @click="handleEdit(scope.row)">编辑</el-button>&ndash;&gt;-->
<!--          <el-popconfirm title="确认删除吗?" @confirm="handleDelete(scope.$index)">-->
<!--            <template #reference>-->
<!--              <el-button type="danger" >删除</el-button>-->
<!--            </template>-->
<!--          </el-popconfirm>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
          :currentPage="currentPage"
          :page-size="pageSize"
          :page-sizes="[5, 10, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog
        v-model="dialogVisible" :title="title" width="30%">
      <el-form :model="priceForm" label-width="120px">
        <el-form-item label="初始费用（0-8小时）">
          <el-input v-model="priceForm.initCost" style="width: 55%;"/>
        </el-form-item>
        <el-form-item label="基本费用（8-24小时）">
          <el-input v-model="priceForm.baseCost" style="width: 55%" />
        </el-form-item>
        <el-form-item label="加收费用（24小时后的每小时）">
          <el-input v-model="priceForm.addCost" style="width: 55%" />
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitPrice">确定</el-button>
      </span>
      </template>
    </el-dialog>

  </div>
</template>

<script>

import request from "../utils/request";

export default {
  name: 'Cabinet',
  components: {
  },
  data() {
    return{
      search:"",
      total:10,
      currentPage:1,
      pageSize:10,
      dialogVisible:false,
      form:{
        not_using:1
      },
      tableData:[],
      priceForm:{
        initCost:"",
        baseCost:"",
        addCost:""
      },
      price:{}
    }
  },

  methods:{
    load(){
      request.get("/cab",{
        params:{
          currentPage: this.currentPage,
          pageSize : this.pageSize,
          search: this.search
        }
      }).then(res => {
        if (res.status === 200){
          console.log(res.data)
          this.tableData = res.data.data.data
          this.total = res.data.data.size
        }
      })
    },
    add(){
      this.dialogVisible = true;
      // console.log()
      this.form = {}
      this.title = "新增存储柜"
    },
    submit(){
      if (this.form.id) {
        // 修改
        request.put("/cab", this.form).then(res => {
          if (res.data.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功！"
            })
            this.dialogVisible = false;
            this.load();
          } else {
            this.$message({
              type: "error",
              message: res.data.msg
            })
          }
        })
      }else {
        // 新增
        request.post("/cab",this.form).then(res =>{
          // this.tableData = res.data
          console.log(res)
          if (res.data.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功！"
            })
            this.dialogVisible = false;
            this.load();
          } else {
            this.$message({
              type: "error",
              message: res.data.msg
            })
          }
        })
      }
    },
    setPrice(){
      this.dialogVisible = true
    },
    getPrice(){
      request.get("/orderings/getPrice").then(res =>{
        this.price = res.data
      })
    },
    submitPrice(){
      request.post("/orderings/setPrice",this.priceForm).then(res =>{
        if (res.data.code === "1"){
          this.dialogVisible = false;
        }else {
          this.$message({
            type: "error",
            message: res.data.msg
          })
        }
        console.log(res.data)
      })
    },
    handleEdit(row){
      // 为避免取消后数据改变
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
      this.title = "编辑存储柜"
    },
    handleDelete(id){
      console.log(id)
      request.delete("/cab/" + id).then(res =>{
        if (res.data.code === '0') {
          this.$message({
            type: "success",
            message: "删除成功！"
          })
          this.load();
        } else {
          this.$message({
            type: "error",
            message: res.data.msg
          })
        }
      })
    },
    // 改变当前每页的个数触发
    handleSizeChange(pageSize){
      this.pageSize = pageSize
      this.load()
    },
    // 改变当前页码触发
    handleCurrentChange(currentPage){
      this.currentPage = currentPage
      this.load()
    },
  },
  created() {
    this.load()
    this.getPrice()
  }
}
</script>
